/*
 * A JSR-168 portlet application for previewing unread
 * email messages.
 *
 * Copyright (C) 2007  San Joaquin Delta College
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 */
package edu.deltacollege.portal.portlets.emailpreview;

import java.util.HashMap;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.mvc.AbstractController;

/**
 * A portlet MVC controller for the Email Preview portlet that retrieves
 * information about the current user's email inbox and dispatches the
 * rendering of that information to a specific view. View dispatching is
 * currently performed using a Spring Web MVC view resolver.
 *
 * @author Andreas Christoforides
 *
 */
public class EmailPreviewController extends AbstractController {

	/**
	 * The name for the Spring Portlet MVC model generated by this controller.
	 */
	public final static String MODEL_NAME = "model";

	private String usernameAttributeName;
	private String passwordAttributeName;
	private String defaultViewName;
	private String userInboxUrl;

	private AccountInfoDAO accountInfoDao;

	/**
	 * Returns the {@link AccountInfoDAO AccountInfoDAO} instance associated
	 * with this controller.
	 *
	 * @return Returns an instance of {@link AccountInfoDAO AccountInfoDAO}.
	 */
	public AccountInfoDAO getAccountInfoDao() {
		return this.accountInfoDao;
	}

	/**
	 * Sets the {@link AccountInfoDAO AccountInfoDAO} instance to be associated
	 * with this controller.
	 *
	 * @param accountInfoDao An instance of {@link AccountInfoDAO AccountInfoDAO}.
	 */
	public void setAccountInfoDao(AccountInfoDAO accountInfoDao) {
		this.accountInfoDao = accountInfoDao;
	}

	/**
	 * Returns the default view name for this controller.
	 *
	 * @return The default view name for this controller.
	 */
	public String getDefaultViewName() {
		return this.defaultViewName;
	}

	/**
	 * Sets the default view name for this controller.
	 *
	 * @param defaultViewName The default view name for this controller.
	 */
	public void setDefaultViewName(String defaultViewName) {
		this.defaultViewName = defaultViewName;
	}

	/**
	 * Returns the name of password user attribute as it is defined in
	 * the portlet deployment descriptor. Portlet user attributes are
	 * retrieved using
	 * <code>(Map) request.getAttribute(PortletRequest.USER_INFO)</code>.
	 * The password user attribute name is used to retrieve the password
	 * of the current user from the map.
	 *
	 * @return Returns the name of the password user attribute.
	 */
	public String getPasswordAttributeName() {
		return this.passwordAttributeName;
	}

	/**
	 * Sets the name of password user attribute as it is defined in
	 * the portlet deployment descriptor. Portlet user attributes are
	 * retrieved using
	 * <code>(Map) request.getAttribute(PortletRequest.USER_INFO)</code>.
	 * The password user attribute name is used to retrieve the password
	 * of the current user from the map.
	 *
	 * @param passwordAttributeName Sets the name of the password user attribute.
	 */
	public void setPasswordAttributeName(String passwordAttributeName) {
		this.passwordAttributeName = passwordAttributeName;
	}

	/**
	 * @return Returns the usernameAttributeName.
	 */
	public String getUsernameAttributeName() {
		return usernameAttributeName;
	}

	/**
	 * @param usernameAttributeName The usernameAttributeName to set.
	 */
	public void setUsernameAttributeName(String usernameAttributeName) {
		this.usernameAttributeName = usernameAttributeName;
	}

	private AccountInfo getAccountInfo (RenderRequest request) throws Exception {

		// Retrieve current user's username and password
		Map userInfo = (Map) request.getAttribute(PortletRequest.USER_INFO);
		String username = (String) userInfo.get(this.usernameAttributeName);
		String password = (String) userInfo.get(this.passwordAttributeName);

		// Get current user's account information
		AccountInfo accountInfo =
				accountInfoDao.retrieveEmailAccountInfo(username, password);

		return accountInfo;
	}

	protected void handleActionRequestInternal(
			ActionRequest request, ActionResponse response) throws Exception {

		// No actions are currently implemented/required
		super.handleActionRequestInternal(request, response);
	}

	protected ModelAndView handleRenderRequestInternal(
			RenderRequest request, RenderResponse response) throws Exception {

		// Retrieve current user's mail account information
		AccountInfo accountInfo = getAccountInfo(request);

		// Define view and generate model
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("accountInfo", accountInfo);
		model.put("inboxUrl", userInboxUrl);
		ModelAndView modelAndView = new ModelAndView(
				this.defaultViewName, MODEL_NAME, model);

		return modelAndView;
	}

	/**
	 * Returns the URL to a user's inbox. The inbox URL is assumed
	 * to be the same for all users. Authentication to the email
	 * account must be performed using an SSO mechanism external to the
	 * email preview portlet.
	 *
	 * @return Returns the URL to the user's inbox.
	 */
	public String getUserInboxUrl() {
		return this.userInboxUrl;
	}

	/**
	 * Sets the URL to a user's inbox. The inbox URL is assumed
	 * to be the same for all users. Authentication to the email
	 * account must be performed using an SSO mechanism external to the
	 * email preview portlet.
	 *
	 * @param userInboxUrl The user inbox URL to set.
	 */
	public void setUserInboxUrl(String userInboxUrl) {
		this.userInboxUrl = userInboxUrl;
	}
}
